<?php

class MODEL_Util{
	
  function gen_random_string($length=8) {

    $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
    $string = '';    

    $n_char_in_collection = strlen($characters);
     
    for ($p = 0; $p < $length; $p++) {
        $string .= $characters[mt_rand(0, $n_char_in_collection-1)];
    }

    return $string;
  }
  


function fsize_unit_convert($bytes){
  $units = array('B', 'KB', 'MB', 'GB');
  $converted = $bytes . ' ' . $units[0];
  for ($i = 0; $i < count($units); $i++){
    if (($bytes/pow(1024, $i)) >= 1){
      $converted = round($bytes/pow(1024, $i), 2) . ' ' . $units[$i];
    }
  }
  return $converted;
}
  
  
  
public function get_ip(){ 
	$ip=false; 
	
	if(!empty($_SERVER["HTTP_CLIENT_IP"])){ 
		$ip = $_SERVER["HTTP_CLIENT_IP"]; 
	} 

	if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { 
		
		$ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']); 
		
		if ($ip) { array_unshift($ips, $ip); $ip = FALSE; } 
			
		for ($i = 0; $i < count($ips); $i++) { 
			if (!eregi ("^(10|172.16|192.168).", $ips[$i])) { 
				$ip = $ips[$i]; 
				break; 
			} 
		} 
	} 
	return ($ip ? $ip : $_SERVER['REMOTE_ADDR']); 
} 
  
  
    function safe_explode($delim, $value, $n_item=2){
      $pos = strpos($value, $delim); # comma in key?
          $a_result = array();
          if ($pos === false){ # no delim found
                array_push($a_result, $value);
          } else {
                $a_result = explode($delim, $value);
          }

          $curr_n_item = count($a_result);
          if ($curr_n_item == $n_item){
                return $a_result;
          }
          if ($curr_n_item > $n_item){
                //echo "too much";
                return array_slice($a_result, 0, $n_item);
          }
          for ($i=$curr_n_item;$i<$n_item; $i++){
                $a_result[$i] = null;
          }

          return $a_result;
     }  
  


  	// Pretty print some JSON
	public function json_format($json)
	{
		$tab = "  ";
		$new_json = "";
		$indent_level = 0;
		$in_string = false;
		
		$len = strlen($json);
		
		
		for($c = 0; $c < $len; $c++) {
			$char = $json[$c];
			switch($char) {
				case '{':
				case '[':
					if(!$in_string) {
						$new_json .= $char . "\n" . str_repeat($tab, $indent_level+1);
						$indent_level++;
					} else {
						$new_json .= $char;
					}
					break;
				case '}':
				case ']':
					if(!$in_string) {
						$indent_level--;
						$new_json .= "\n" . str_repeat($tab, $indent_level) . $char;
					} else {
						$new_json .= $char;
					}
					break;
				case ',':
					if(!$in_string) {
						$new_json .= ",\n" . str_repeat($tab, $indent_level);
					} else {
						$new_json .= $char;
					}
					break;
				case ':':
					if(!$in_string) {
						$new_json .= ": ";
					} else {
						$new_json .= $char;
					}
					break;
				case '"':
					if($c > 0 && $json[$c-1] != '\\') {
						$in_string = !$in_string;
					}
				default:
					$new_json .= $char;
					break;					
			}
		}
		
		return $new_json;
	}
  
  
}

?>